package com.ck.ck_back.service.impl;

import com.ck.ck_back.dao.FocusMapper;
import com.ck.ck_back.pojo.Focus;
import com.ck.ck_back.pojo.Result;
import com.ck.ck_back.service.focusservice;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
@Service
public class focusserviceImpl implements focusservice {
    @Resource
    private FocusMapper focusMapper;

//    获得数量
    @Override
    public int getfocus_num(String master_id, String post_id) {
        if(master_id!=null&&!master_id.equals("")&&post_id!=null&&!post_id.equals("")){
            List<Focus> focusList=focusMapper.selectfoucs_num(master_id,post_id);
            return focusList.size();
        }else {return 404;}
    }

    @Override
    public List<Focus> getfocus(String master_id) {
        return focusMapper.selectfoucs(master_id);
    }

    @Override
    public int get_state(String master_id, String post_id) {
        if(master_id!=null&&!master_id.equals("")&&post_id!=null&&!post_id.equals("")){
            Focus focus = focusMapper.getcontent(master_id,post_id);
            if (focus!=null){
                return focus.getState();
            }else {return 0;}
        }else {return 404;}
    }

    @Override
    public int update(String master_id, String post_id) {
        if(master_id!=null&&!master_id.equals("")&&post_id!=null&&!post_id.equals("")){
            Focus focus=focusMapper.getcontent(master_id, post_id);
            if (focus!=null){
                if (focus.getState() == 0){
                    focus.setState(1);
                    focus.setFocustime(new Date().getTime());
                }else {focus.setState(0);}
                int result = focusMapper.updateByPrimaryKeySelective(focus);
                if (result ==1){
                    return result;
                }else {return 2;}
            }else {return 3;}
        }else {return 404;}
    }

    @Override
    public int add(String master_id, String post_id) {
        if(master_id!=null&&!master_id.equals("")&&post_id!=null&&!post_id.equals("")){
            Focus focus=focusMapper.getcontent(master_id, post_id);
            if (focus == null){
                Focus focus1 = new Focus();
                focus1.setMasterId(master_id);
                focus1.setPostId(post_id);
                focus1.setFocustime(new Date().getTime());
                focus1.setState(1);
                int result =focusMapper.insertSelective(focus1);
                if(result==1){
                    return result;
                }else {return 2;}
            }else {return 3;}
        }else {return 404;}
    }

    @Override
    public int delectfocus(int focus_id) {
        if (!String.valueOf(focus_id).equals("")){
            Focus focus = focusMapper.selectByPrimaryKey(focus_id);
            if (focus!=null){
                focus.setState(0);
                int result = focusMapper.updateByPrimaryKeySelective(focus);
                if (result == 1){
                    return result;
                }else {return 2;}
            }else {return 3;}
        }else {return 404;}
    }



}
